﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Net.Mail;
using System.ComponentModel;

namespace qna.Business
{
    public class subscription : BusinessObject
    {
        public int Id { get; set; }
        public int QnId { get; set; }

        protected override string PrimaryKey
        {
            get
            {
                return "user_id";
            }
        }
        protected override string TableName
        {
            get { return "subscription"; }
        }

        protected override BusinessObject New()
        {
            throw new NotImplementedException();
        }

        public override IDictionary<string, object> Attributes
        {
            get
            {
                IDictionary<string, object> attrs = new Dictionary<string, object>();
                attrs["user_id"] = Id;
                attrs["question_id"] = QnId;
                return attrs;
            }
        }

        public override void ReadAttributes(System.Data.SqlClient.SqlDataReader dr)
        {
            throw new NotImplementedException();
        }

        
        public void SendMail(int id)
        {
            IList<string> results = new List<string>();
            string sql = "SELECT email FROM subscription inner join [user] on user_id=id where question_id=@id";
            IDictionary<string, object> param = new Dictionary<string, object>();
            param.Add("id", id);
            ExecuteReader(sql, param, delegate(SqlDataReader dr)
            {
                while (dr.Read())
                {
                    results.Add(dr[0].ToString());
                }
                return dr;
            });
            foreach (var item in results)
            {
               string uname = WebConfigurationManager.AppSettings["eUser"];
               string upass = WebConfigurationManager.AppSettings["ePass"];

               MailMessage mailMessage = new MailMessage(new MailAddress("xjapanislove@gmail.com")
                                     , new MailAddress(item));
               mailMessage.Subject = "New Answer";
               mailMessage.IsBodyHtml = true;
               mailMessage.Body = "Please check QnA site for updates on your subscribed question.";

               System.Net.NetworkCredential networkCredentials = new
               System.Net.NetworkCredential(uname, upass);

               SmtpClient smtpClient = new SmtpClient();
               smtpClient.EnableSsl = true;
               smtpClient.UseDefaultCredentials = false;
               smtpClient.Credentials = networkCredentials;
               smtpClient.Host = "smtp.gmail.com";
               smtpClient.Port = 587;
               smtpClient.Send(mailMessage);
 
            }
        }
        
        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public bool Delete(int QnId, int UId)
        {
            string sql = "DELETE FROM subscription WHERE question_id=@QnId AND user_id=@uId";
            IDictionary<string, object> param = new Dictionary<string, object>();
            param.Add("QnId", QnId);
            param.Add("UId", UId);
            return ExecuteNonQuery(sql, param) > 0;
        }

    }
}